const express = require('express');
const router = express.Router();

// 实体类
const Login = require('../entity/login');
const Response = require('../entity/response');

// 业务层
const loginService = require('../service/login');

// 用户名密码登录接口
router.post('/login', async function(req, res, next) {
    // 判断参数是否合法
    let login;
    try {
        login = new Login(req.body);
        login.check();
    } catch (err) {
        return res.status(400).json(err);
    }
    // 调用业务层
    loginService.login(login).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("登录失败"));
    });
});

module.exports = router;